/*
 * File:   not_Bcoode.c
 * Author: luca
 *
 * Created on 25 dicembre 2011, 22.02
 */

#include <p18f4550.h>
#include <spi.h>
#include <delays.h>

#pragma config FOSC = XT_XT
#pragma config PBADEN= OFF
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PLLDIV = 1
#pragma config MCLRE = ON
#pragma config CPUDIV = OSC1_PLL2

#pragma config USBDIV = 1
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config VREGEN = OFF
#pragma config CCP2MX = OFF
#pragma config LPT1OSC = OFF
#pragma config STVREN = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF

#define max7219_reg_noop         0x00
#define max7219_reg_digit0       0x01
#define max7219_reg_digit1       0x02
#define max7219_reg_digit2       0x03
#define max7219_reg_digit3       0x04
#define max7219_reg_digit4       0x05
#define max7219_reg_digit5       0x06
#define max7219_reg_digit6       0x07
#define max7219_reg_digit7       0x08
#define max7219_reg_decodeMode   0x09
#define max7219_reg_intensity    0x0a
#define max7219_reg_scanLimit    0x0b
#define max7219_reg_shutdown     0x0c
#define max7219_reg_displayTest  0x0f

#define LOAD PORTAbits.RA5

void init(void);
void delay1s(void); //verificato!! 1 secondo!
void delay250ms(void);
void delay125ms(void);
void delayI( long i);
void spegniDigit(int i);

void main(void) {
    int i=0,j=0, k=0, z=0;
     long del=0;

    TRISAbits.RA5=0; //ouput
    ADCON0=0;
    ADCON1=1;
    ADCON2=0;

    LOAD=0;
    OpenSPI(SPI_FOSC_4, MODE_00, SMPEND);
    LOAD=1;
    LOAD=0;

    init();

    //del=255;
    del=3000;

    while(1){

        

           /* LOAD=0;
            WriteSPI(0x01);
            WriteSPI(0b00000001);
            LOAD=1;
            LOAD=0;

            delay250ms();

            LOAD=0;
            WriteSPI(0x01);
            WriteSPI(0b00000010);
            LOAD=1;
            LOAD=0;

            delay250ms();

            LOAD=0;
            WriteSPI(0x01);
            WriteSPI(0b00000100);
            LOAD=1;
            LOAD=0;

            delay250ms();

            LOAD=0;
            WriteSPI(0x01);
            WriteSPI(0b00001000);
            LOAD=1;
            LOAD=0;

            delay250ms();

            LOAD=0;
            WriteSPI(0x01);
            WriteSPI(0b00010000);
            LOAD=1;
            LOAD=0;

            delay250ms();

            LOAD=0;
            WriteSPI(0x01);
            WriteSPI(0b00100000);
            LOAD=1;
            LOAD=0;

            delay250ms();

            LOAD=0;
            WriteSPI(0x01);
            WriteSPI(0b01000000);
            LOAD=1;
            LOAD=0;

            delay250ms();*/


            /*for(i=2;i<=64;i=i*2){

                LOAD=0;
                WriteSPI(0x01);
                WriteSPI(i);
                LOAD=1;
                LOAD=0;
             * delayI(del);


            }*/
                //digit1
                LOAD=0;
                WriteSPI(0x01);
                WriteSPI(8);
                LOAD=1;
                LOAD=0;
                delayI(del);

                

                LOAD=0;
                WriteSPI(0x01);
                WriteSPI(16);
                LOAD=1;
                LOAD=0;
                delayI(del);

                LOAD=0;
                WriteSPI(0x01);
                WriteSPI(32);
                LOAD=1;
                LOAD=0;
                delayI(del);

                LOAD=0;
                WriteSPI(0x01);
                WriteSPI(64);
                LOAD=1;
                LOAD=0;
                delayI(del);

                spegniDigit(0x01);

                //digit2

                LOAD=0;
                WriteSPI(0x02);
                WriteSPI(64);
                LOAD=1;
                LOAD=0;
                delayI(del);

                spegniDigit(0x02);

                //digit 3

                LOAD=0;
                WriteSPI(0x03);
                WriteSPI(64);
                LOAD=1;
                LOAD=0;
                delayI(del);

                spegniDigit(0x03);
                
                //digit4

                LOAD=0;
                WriteSPI(0x04);
                WriteSPI(64);
                LOAD=1;
                LOAD=0;
                delayI(del);

                LOAD=0;
                WriteSPI(0x04);
                WriteSPI(2);
                LOAD=1;
                LOAD=0;
                delayI(del);

                LOAD=0;
                WriteSPI(0x04);
                WriteSPI(4);
                LOAD=1;
                LOAD=0;
                delayI(del);

                LOAD=0;
                WriteSPI(0x04);
                WriteSPI(8);
                LOAD=1;
                LOAD=0;
                delayI(del);

                spegniDigit(0x04);


                //digit 3
                LOAD=0;
                WriteSPI(0x03);
                WriteSPI(8);
                LOAD=1;
                LOAD=0;
                delayI(del);

                spegniDigit(0x03);

                //digit2

                LOAD=0;
                WriteSPI(0x02);
                WriteSPI(8);
                LOAD=1;
                LOAD=0;
                delayI(del);

                spegniDigit(0x02);

        
            if(del>2000)del=del-300;
            if(del>1000)del=del-100;
            if(del>600)del=del-70;
            if(del>200)del=del-10;

            if (del<=0) del=3000;

    }



}



void delay1s(void){

    Delay10KTCYx(100);



}

void delay250ms(void){

    Delay10KTCYx(25);


}
void delay125ms(void){

    Delay10KTCYx(6);


}

void delayI( long i){

    //Delay100TCYx(i);

     int j=0;
    for(j=0;j<i;j++){

        Nop();

    }//for
    
}




void spegniDigit(int i){

    
    LOAD=0;
    WriteSPI(i);
    WriteSPI(0x00);
    LOAD=1;
    LOAD=0;
    
    


}

void init(void)
{
    int k = 0;
LOAD = 0;       // SELECT MAX
WriteSPI(0x09);      // BCD mode for digit decoding
WriteSPI(0x00);
LOAD = 1;       // DESELECT MAX
LOAD = 0;


LOAD = 0;       // SELECT MAX
WriteSPI(0x0A);
WriteSPI(0x0F);      // Segment luminosity intensity
LOAD = 1;       // DESELECT MAX
LOAD = 0;


LOAD = 0;       // SELECT MAX
WriteSPI(0x0B);
WriteSPI(0x07);      // Display refresh
LOAD = 1;       // DESELECT MAX
LOAD = 0;


LOAD = 0;       // SELECT MAX
WriteSPI(0x0C);
WriteSPI(0x01);      // Turn on the display
LOAD = 1;       // DESELECT MAX
LOAD = 0;




LOAD = 0;       // SELECT MAX
WriteSPI(0x0F);
WriteSPI(0x0);      // No test
LOAD = 1;       // DESELECT MAX
LOAD = 0;


for(k=1;k<5;k++){
        LOAD=0;
        WriteSPI(k);
        WriteSPI(0x00);
        LOAD=1;
     }
}